package net.zeus.sample.rabbitmq.workfair;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import net.zeus.sample.rabbitmq.utils.ConnectionUtils;

public class WorkFairMqSender {

	public static void main(String[] args) throws Exception {
		Connection conn = ConnectionUtils.getConnection();
		
		Channel channel = conn.createChannel();
		
		channel.queueDeclare(ConnectionUtils.WORK_QUEUE_NAME, false, false, false, null);
		
		/* 
		 * 每个消费者 发送确认消息之前，消息队列不发送下一个消息，一次只处理一个消息
		 * 
		 * 限制发送同一个消费者不得超过一条消息
		 */
		channel.basicQos(1);
		
		for (int i = 0; i < 50; i++) {
			String msg = "hello work test[" + i + "]";
			
			channel.basicPublish("", ConnectionUtils.WORK_QUEUE_NAME, null, msg.getBytes());
			System.out.println("send msg:" + msg);
			
			Thread.sleep(i * 20);
		}
		
		channel.close();
		ConnectionUtils.closeConnection(conn);
	}

}
